<template>
  <view class="container">
    <view class="main">
      <view v-for="(item, index) in list" :key="index">{{ item }}</view>
    </view>

    <view class="navbar">
      <input type="text" class="content" v-model="content" />
      <button @click="send">发送</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      content: '',
    }
  },
  onLoad() {
    this.getMsg()
  },
  methods: {
    send() {
      if (this.content.length > 0) {
        this.list.push(this.content)
        let msg = this.content
        this.$socket.emit('message', msg)
        this.content = ''
      } else {
        console.log('不能为空')
      }
    },
    getMsg() {
      this.$socket.on('msg', (data) => {
        this.list.push(data)
      })
    },
  },
}
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.title {
  font-size: 36rpx;
  color: #8f8f94;
}
.navbar {
  width: 100%;
  position: fixed;
  bottom: 100rpx;
}
.content {
  width: 100%;
  height: 80rpx;
  background: #eee;
}
</style>
